import dayjs from "../../libs/dayjs";
import { AstContext } from "../context";
import { getSafeClip } from "../utils/getSafeClip";
import { parseShareLink } from "../utils/parseShareLink";
import { useRoot } from "../utils/useRoot";
import { AstVisitor, visit } from "../visitor";

// id("live_bottom_bar_single_icon_container")
// id("live_bottom_bar_icon")
export class ShareLinkVisitor extends AstVisitor {
    visitRelativeLayout(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        // id("live_new_bottom_bar_container")
        if (id) {
            if (id.endsWith('live_new_bottom_bar_container') && node.visibleToUser()) {

            }
        }
    }
    // className("android.widget.LinearLayout")
    visitLinearLayout(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        // id("live_bottom_bar_left_layout")
        if (id) {
            if (id.endsWith('live_bottom_bar_left_layout') && node.visibleToUser()) {
                const nodes = ctx.filter(node, it => {
                    const id = it.id()
                    if (!id) return false
                    if (id.endsWith('live_bottom_bar_single_icon_container') && it.clickable()) {
                        return true
                    }
                    return false
                })
                if (nodes.length > 0) {
                    const status = ctx.get<string>('status')
                    if (status !== 'open') {
                        nodes[0].click()
                        ctx.set('status', 'open')
                        sleep(1000)
                    }
                }else{
                    console.log(id)
                }
            }
        }
    }
    visitFrameLayout(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        if (id) {
            if (id.endsWith('live_play_root_container') && node.visibleToUser()) {
                const nodes = ctx.filterChildren(node, it => {
                    const id = it.id()
                    return (it.className() === 'android.widget.RelativeLayout') && !!id && id.endsWith('live_bottom_bar_single_icon_container')
                })
                if (nodes.length > 0) {
                    const status = ctx.get<string>('status')
                    if (status !== 'open') {
                        nodes[0].click()
                        ctx.set('status', 'open')
                        sleep(1000)
                    }
                }
                nodes.map(node => node.recycle())
            }
        }
        return false;
    }
    // id("live_bottom_bar_single_icon_container")
    visitTextView(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        const text = node.text()
        if (id && text) {
            if (id.endsWith('live_bottom_bar_item_title_text_view') && text === '分享') {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    clickable.click()
                    clickable.recycle()
                    sleep(1000)
                }
                ctx.set('status', 'share')
                sleep(1000)
            }
            else if (text === '复制链接' && id.endsWith('title')) {
                const clickable = ctx.findInParent(node, it => it.clickable())
                if (clickable) {
                    clickable.click()
                    clickable.recycle()
                    ctx.set('status', 'copy')
                    sleep(1000)
                }
            }
        }
        return false
    }

    visitImageView(node: AutoJs.UiObject, ctx: AstContext) {
        const id = node.id()
        if (id) {
            if (id.endsWith('live_red_packet_container_close_view')) {
                node.click()
                sleep(1000)
            }
            if (id.endsWith('close_view')) {
                node.click()
                sleep(1000)
            }
        }
    }
}

export const shareLinkVisitor = new ShareLinkVisitor()

export function getShareLink(start: boolean) {
    const ctx = new AstContext(dayjs)
    try {
        let status = ctx.get<string>('status') || 'finding'
        while (status !== 'copy' && start) {
            const root = useRoot()
            if(!root) break;
            shareLinkVisitor.visit(root, ctx)
            root.recycle()
            status = ctx.get<string>('status') || 'finding'
            sleep(1000)
        }
        console.log(`复制链接成功`)
        sleep(2000)
        const link = getSafeClip()
        if (!link) {
            toastLog("获取分享链接失败")
            return ctx;
        }
        console.log(`分享链接：${link}`)
        const res = link.match(/(http.*?) (.*?)的直播/)
        if (res && res.length > 0) {
            const [, link, name] = res
            ctx.set('link', link)
            ctx.set('name', name)
            const parseResult = parseShareLink(link, name)
            ctx.set('share', parseResult)
            console.log(`分享链接解析成功`)
        }
        return ctx;
    } catch (e) {
        console.log(e)
        return ctx
    }
}